package com.joysuch.wwyt.core.common.ajax;


import com.joysuch.wwyt.core.enums.Status;
import lombok.Data;

import java.text.MessageFormat;

/**
 * result
 *
 * @param <T> T
 */
@Data
public class Result<T> {
    /**
     * status
     */
    private Integer code;

    /**
     * message
     */
    private String errMsg;

    /**
     * data
     */
    private T data;

    public Result(){}

    public Result(Integer code , String errMsg){
        this.code = code;
        this.errMsg = errMsg;
    }
    /**
     * 失败返回结果
     * @param errMsg 提示信息
     */
    public static <T> Result<T> failed(String errMsg) {
        return new Result<T>(Status.FAILED.getCode(), errMsg);
    }

    public static <T> Result<T> setReturn(Status status, Object... statusParams){
        if (statusParams != null && statusParams.length > 0) {
            return new Result<T>(status.getCode(), MessageFormat.format(status.getMsg(), statusParams));
        }
        else{
            return new Result<T>(status.getCode(), status.getMsg());
        }
    }
}
